<?php
namespace Admin\Controller;
use Common\Controller\BaseController;
use Think\Controller;

class IndexController extends BaseController {

    /**
     * issac-note官网
     * */
    public function index(){


        //$this->register("issac", "123456", "752602916@qq.com");
        $this->display();
        
    }

    /**
     * 登陆
     * */
    public function login($user, $pwd){
        
        if($user ==""|| $pwd=="") {
            
            $this->ajaxMsg(1,"user or pwd undefined");
            
        }
        
        $userMsg =M("User")->where(array('username'=>$user,'status'=>1))->find();

        if($userMsg) {

            if(!$userMsg['status']) {

                $this->ajaxMsg(1, "not active");

            }

            if(md5($pwd) == $userMsg['password']) {

                $this->ajaxMsg(0, "success");

            }else {

                $this->ajaxMsg(1, "fail");

            }

        }

    }

    /**
     * 注册
     * */
    public function register() {

        $username =$_REQUEST['user'];
        $password =$_REQUEST['pwd'];
        $address  =$_REQUEST['email'];

        if(
            $username == "" ||
            $password == "" ||
            $address  == ""
        ) {

            $this->ajaxMsg(1, "info is null");

        }

        if($this->userIsExist($username)){

            $this->ajaxMsg(1, "用户名已存在");

        }

        if(!$uid =$this->insertNewUser($username, $password, $address)){

            $this->ajaxMsg(1, "fail insert");

        }

        $title   ="Issac-Note笔记";

        $url =C("HOST") .__ROOT__."/Admin/Index/activeAccount?issacnote=$uid";

        $message ="感谢使用issac-note笔记，请点击下方链接激活：\n".$url."\n该链接仅在24小时内有效！";

        $res =$this->sendMail($address, $title, $message);

        if($res) {

            $this->ajaxMsg(0, "success");

        }else {

            $this->ajaxMsg(1, "fail send mail");

        }

    }

    /**
     * 检测用户名已经存在
     * @param  $username
     * @return boolean
     * */
    public function userIsExist($username){

        return M("User")->where(array('username'=>$username))->getField('username');

    }


    /**
     * 新增新用户
     * @param  $username
     * @param  $pwd
     * @param  $email
     * @return boolean
     * */
    private function insertNewUser($username, $pwd, $email){

        $res =M("User")->add(array(

            'username'      =>$username,
            'password'      =>md5($pwd),
            'email'         =>$email,
            'limit_time'    =>time() +24 *60 *60

        ));

        if($res) {

            return M("User")->where(array('username'=>$username))->getField("id");

        }else {

            return 0;

        }

    }


    /**
     * 激活用户
     * */
    public function activeAccount() {

        $where['id'] =I("get.issacnote");

        $limitTime =M("User")->where($where)->getField("limit_time");

        if($limitTime <time()) {

            $this->display('active_fail');

        }

        if (M("User")->where($where)->save(array('status'=>1)) ) {

            $this->display('active_success');

        }

    }


    /**
     * 根据用户uid获取日志数据集
     * @param   $uid
     * */
    public function getNotesByUid($uid) {

        header("content-type:text/html;charset=utf-8;");
        
        $noteList =M("List")->where(array('uid'=>$uid,'status'=>1))->select();

        //echo "<pre>";print_r($noteList);echo "</pre>";

        if($noteList) {

            $this->ajaxMsg(0, "success", $noteList);

        }else {

            $this->ajaxMsg(1, "fail");

        }
        
    }

    /**
     * 新建日志
     * @param   $noteMsg （日志数据集）
     * */
    public function insertNote($noteMsg) {

        $uid   =I("post.uid");
        $title =I("post.title");
        $ctt   =$_POST['ctt'];

        $res =M("List")->add(array(
            'uid'=>$uid,
            'title'=>$title,
            'content'=>$ctt,
            'create_time'=>time()
        ));

        if($res) {

            $this->ajaxMsg(0, "success");

        }else {

            $this->ajaxMsg(1, "fail");

        }

    }

    /**
     * 更新日志内容
     * @param   $noteMsg （日志数据集）
     * */
    public function updateNote($noteMsg) {

        $noteId =I("post.noteId");
        $title  =I("post.title");
        $ctt    =$_POST['ctt'];

        $res =M("List")->where(array('id'=>$noteId))->save(array(
            'title'=>$title,
            'content'=>$ctt,
            'update_time'=>time()
        ));

        if($res) {

            $this->ajaxMsg(0, "success");

        } else{

            $this->ajaxMsg(1, "fail");

        }

    }

    /**
     * 删除日志
     * @param   $note_id （日志id）
     * @param   $isDelete =0
     * */
    public function deleteNote($note_id, $isDelete =0) {

        if(is_array($note_id)){

            $where['id'] =array('in', array('in', $note_id));

        }else {

            $where['id'] =$note_id;

        }

        if($isDelete) {

            $res =M("list")->where($where)->delete();

        }else {

            $res = M("list")->where($where)->save(array('status' => 0));

        }

        $status =$res ?0 :1;

        $this->ajaxMsg($status, "delete");

    }
}